package com.forsythia.sns.dao;

import java.util.List;

import org.hibernate.Query;

import com.forsythia.sns.to.FriendTO;
import com.forsythia.sns.to.SignatureTO;
import com.forsythia.sns.to.UserTO;

public class UserDAOImpl extends BaseDAOImpl implements UserDAO {

    @Override
    public boolean isEmailExists(String email) {
        return this.findByProperty(UserTO.class, "email", email) != null;
    }

    @Override
    public boolean isLoginNameExists(String loginName) {
        return this.findByProperty(UserTO.class, "loginName", loginName) != null;
    }
    
    @Override
    public List<UserTO> searchUserTOByName(String userName){
        return this.findByProperty(UserTO.class, "userName", userName);
    }
    
    @Override
    public SignatureTO getLastSignature(String userId){
        String queryString = "from SignatureTO as model where model.user.userId= ? order by createdDate desc";
        Query queryObject = getSession().createQuery(queryString);
        queryObject.setParameter(0, userId);
        List<SignatureTO> signatureList = queryObject.list();
        if(signatureList != null && signatureList.size() > 0){
            signatureList.get(0);
        }
        return null;
    }
    
    public List<FriendTO> getFirendList(){
        List<FriendTO> friendList = null;
        return null;
    }

    @Override
    public List<UserTO> getFriends(String userId, int firstResult, int fetchSize) {
        String queryString = "select UserTO from FriendTO as friend join UserTO as user where user.userId= :userId and user.userId = friend.userId";
        Query queryObject = getSession().createQuery(queryString);
        queryObject.setFirstResult(firstResult);
        queryObject.setFetchSize(fetchSize);
        queryObject.setParameter("userId", userId);
        List<UserTO> friendsList = queryObject.list();
        return friendsList;
    }

}
